<?php

/**
 * ECSHOP 夺宝奇兵管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: snatch.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
// $exc = new exchange($ecs->table("goods_activity"), $db, 'act_id', 'act_name');
/*zy*/
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'default';
/*------------------------------------------------------ */
//-- 报名列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      $_LANG['sign_lists']);
    $smarty->assign('action_link',  array('text' => $_LANG['sign_add'], 'href'=>'sign.php?act=add'));

    $snatchs = get_signlist();
    
    // snatch_list
    foreach ($snatchs['snatchs'] as $key => $value) {
       $snatchs['snatchs'][$key]['sign_time'] = local_date('Y-m-d H:i', $value['sign_time']);
       $snatchs['snatchs'][$key]['audit_time'] = local_date('Y-m-d H:i', $value['audit_time']);
       /*获取所报活动名称*/
        $sql = "SELECT activity_name ".
               " FROM " . $GLOBALS['ecs']->table('activity') .  " WHERE activity_id = $value[activity_id]";
        $snatchs['snatchs'][$key]['activity_name'] = $GLOBALS['db']->getOne($sql);

        /*转换审核状态*/
        switch ($snatchs['snatchs'][$key]['audit_status']) {
            case '0':
                $snatchs['snatchs'][$key]['audit_status'] = '未审核';
                break; 
            case '1':
                $snatchs['snatchs'][$key]['audit_status'] = '未审核';
                break;

            case '2':
                $snatchs['snatchs'][$key]['audit_status'] = '审核通过';
                break;

            case '3':
                $snatchs['snatchs'][$key]['audit_status'] = '审核未通过';
                break;
            
            default:
                $value['audit_status'] = '请选择';
                break;
        }

    }
    // echo "<pre>";
    // print_r( $snatchs['snatchs']);
    // echo "</pre>";
    // die();
    $smarty->assign('snatch_list',  $snatchs['snatchs']);
    $smarty->assign('filter',       $snatchs['filter']);
    $smarty->assign('record_count', $snatchs['record_count']);
    $smarty->assign('page_count',   $snatchs['page_count']);

    $sort_flag  = sort_flag($snatchs['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    $smarty->assign('full_page',    1);
    assign_query_info();
    $smarty->display('sign_list.htm');
}
/*------------------------------------------------------ */
//-- 编辑报名信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('sign_manage');

    $snatch = get_snatch_info($_REQUEST['sign_id']);

    /*把所有审核状态组成一个数组*/
    $audit_arr = array('未审核','审核通过', '审核未通过');



    $smarty->assign('snatch', $snatch);
    $smarty->assign('audit_arr',  $audit_arr);
   
    $smarty->assign('ur_here',              $_LANG['snatch_edit']);
    $smarty->assign('action_link',          array('text' => $_LANG['sign_add'], 'href'=>'sign.php?act=list&' . list_link_postfix()));
    $smarty->assign('form_action',        'update');

    /* 商品货品表 */
    $smarty->assign('good_products_select', get_good_products_select($snatch['goods_id']));

    assign_query_info();
    $smarty->display('sign_info.htm');
}
elseif ($_REQUEST['act'] =='update')
{
    /* 权限判断 */
    admin_priv('snatch_manage');
    /*获取当前时间即审核时间*/
    $showtime=date("Y-m-d H:i:s");
    $NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;

    $sign['sign_phone']=$_POST['sign_phone'];
    $sign['sign_work']=$_POST['sign_work'];
    $sign['audit_id']=$_POST['audit_id'];
    $sign['audit_content']=$_POST['audit_content'];

     /* 更新数据 */
     $sql = "UPDATE " .$ecs->table('signinfo'). " SET 
                sign_phone = '$sign[sign_phone]' , 
                sign_work = '$sign[sign_work]', 
                audit_status = '$sign[audit_id]', 
                audit_time = '$NowTime', 
                audit_content = '$sign[audit_content]'            
                WHERE sign_id = '$_REQUEST[sign_id]'";
    $GLOBALS['db']->query($sql);

    $link[] = array('text' => $_LANG['back_list'], 'href'=>'sign.php?act=list&' . list_link_postfix());
    sys_msg($_LANG['edit_succeed'],0,$link);
 }

/*------------------------------------------------------ */
//-- 删除指定的报名信息
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('attr_manage');

    $id = intval($_GET['id']);
     $cat_name = $db->getOne('SELECT sign_name FROM ' .$ecs->table('signinfo'). " WHERE sign_id='$id'");
  $sql = 'DELETE FROM ' .$ecs->table('signinfo'). " WHERE sign_id = '$id'";
        if ($db->query($sql))
        {
            $db->query("DELETE FROM " . $ecs->table('nav') . "WHERE ctype = 'c' AND cid = '" . $cat_id . "' AND type = 'middle'");
            clear_cache_files();
            admin_log($cat_name, 'remove', 'signinfo');
        }

    // $exc->drop($id);

    $url = 'sign.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}
/*------------------------------------------------------ */
//-- 查询、翻页、排序
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $snatchs = get_signlist();

    // snatch_list
    foreach ($snatchs['snatchs'] as $key => $value) {
       $snatchs['snatchs'][$key]['sign_time'] = local_date('Y-m-d H:i', $value['sign_time']);
       $snatchs['snatchs'][$key]['audit_time'] = local_date('Y-m-d H:i', $value['audit_time']);
       /*获取所报活动名称*/
        $sql = "SELECT activity_name ".
               " FROM " . $GLOBALS['ecs']->table('activity') .  " WHERE activity_id = $value[activity_id]";
        $snatchs['snatchs'][$key]['activity_name'] = $GLOBALS['db']->getOne($sql);

        /*转换审核状态*/
        switch ($snatchs['snatchs'][$key]['audit_status']) {
            case '0':
                $snatchs['snatchs'][$key]['audit_status'] = '未审核';
                break; 
            case '1':
                $snatchs['snatchs'][$key]['audit_status'] = '未审核';
                break;

            case '2':
                $snatchs['snatchs'][$key]['audit_status'] = '审核通过';
                break;

            case '3':
                $snatchs['snatchs'][$key]['audit_status'] = '审核未通过';
                break;
            
            default:
                $value['audit_status'] = '请选择';
                break;
        }

    }

    
    $smarty->assign('snatch_list',  $snatchs['snatchs']);
    $smarty->assign('filter',       $snatchs['filter']);
    $smarty->assign('record_count', $snatchs['record_count']);
    $smarty->assign('page_count',   $snatchs['page_count']);

    $sort_flag  = sort_flag($snatchs['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('sign_list.htm'), '', array('filter' => $snatchs['filter'], 'page_count' => $snatchs['page_count']));
}
elseif($act == 'search'){
    // $key = $_POST['keywords'];
    include_once('../includes/cls_json.php');
    $json = new JSON;
    $keywords = $_REQUEST['keywords'];
    $sql = "SELECT * " .
           " FROM " . $GLOBALS['ecs']->table('signinfo') . " WHERE sign_name='$keywords' ";
    $search = $GLOBALS['db']->GetRow($sql);
    
    $smarty->assign('search', $search);

            
    // $all['content'] = $smarty->fetch('library/sign.lbi');
    $back['cotent'] = $smarty->fetch('sign.htm');
    //     echo "<pre>";
    // print_r($search);
    // echo "</pre>";
    // die("00"); 
    // die($json->encode($all['content']));
    die($json->encode($back['cotent']));
    // die($json->encode($search));

}

/*获取报名列表*/
function get_signlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 查询条件 */
        $filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        //echo $keywords;
         if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
         {
             $filter['keywords'] = json_str_iconv($filter['keywords']);
         }
         
        $where  = " where  1 "; 
        // $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'act_id' : trim($_REQUEST['sort_by']);
        // $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        
        if($filter['keywords']){
            
            $where .=(!empty($filter['keywords'])) ? " AND sign_name like '%". mysql_like_quote($filter['keywords']) ."%'" : '';
            
        }
        
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('signinfo') . $where;
        
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 获取所有报名列表数据 */
        $sql = "SELECT * "." FROM " . $GLOBALS['ecs']->table('signinfo'). $where ." ORDER BY sign_id desc LIMIT ". $filter['start'] .", " . $filter['page_size'];

         $filter['keywords'] = stripslashes($filter['keywords']);
         set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $row = $GLOBALS['db']->getAll($sql);

    // foreach ($row AS $key => $val)
    // {
    //     $row[$key]['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $val['start_time']);
    //     $row[$key]['end_time']   = local_date($GLOBALS['_CFG']['time_format'], $val['end_time']);
    //     $info = unserialize($row[$key]['ext_info']);
    //     unset($row[$key]['ext_info']);
    //     if ($info)
    //     {
    //         foreach ($info as $info_key => $info_val)
    //         {
    //             $row[$key][$info_key] = $info_val;
    //         }
    //     }
    // }

    $arr = array('snatchs' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
    // echo "<pre>";
    // print_r($arr);
    // echo "</pre>";
    // die();
}
/**
 * 获取指定id 的报名信息
 *
 * @access  public
 * @param   int         $id         snatch_id
 *
 * @return array       array(snatch_id, snatch_name, goods_id,start_time, end_time, min_price, integral)
 */
function get_snatch_info($id)
{
    // global $ecs, $db,$_CFG;

    $sql = "SELECT * " . " FROM " . $GLOBALS['ecs']->table('signinfo') . " WHERE sign_id='$id' ";
           $snatch = $GLOBALS['db']->GetRow($sql);

    /* 将时间转成可阅读格式 */
    $snatch['sign_time'] = local_date('Y-m-d H:i', $snatch['sign_time']);
    $snatch['audit_time']   = local_date('Y-m-d H:i', $snatch['audit_time']);
    /*转换审核状态*/
    switch ($snatch['audit_status']) {
        case '0':
            $snatch['audit_status_name'] = '未审核';
            break;
            
        case '1':
            $snatch['audit_status_name'] = '未审核';
            break;

        case '2':
            $snatch['audit_status_name'] = '审核通过';
            break;

        case '3':
            $snatch['audit_status_name'] = '审核未通过';
            break;
        
        default:
            $snatch['audit_status_name'] = '请选择';
            break;
    }
    /*获取所报活动名称*/
        $sql = "SELECT activity_name ".
               " FROM " . $GLOBALS['ecs']->table('activity') .  " WHERE activity_id = $snatch[activity_id]";
        $snatch['activity_name'] = $GLOBALS['db']->getOne($sql);

    return $snatch;
}

?>